package 越长越合法

func countCompleteSubarrays(nums []int) int {
	diff := map[int]int{}
	for _, num := range nums {
		diff[num]++
	}
	n := len(diff)
	total := len(nums) * (len(nums) + 1) / 2
	freq := map[int]int{}
	left := 0
	ans := 0
	for right, num := range nums {
		freq[num]++
		for len(freq) == n && left <= right {
			freq[nums[left]]--
			if freq[nums[left]] == 0 {
				delete(freq, nums[left])
			}
			//注意位置
			left++

		}
		ans += right - left + 1
	}
	return total - ans
}
